<?php
namespace app\shop\model;
use app\rbac\model\AdminM;
use think\Request;
use think\Validate;
use think\Db;
use app\base\model\Base;
class ShopM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'title' => 'require',
               'shop_intro' => 'require',
//               'content' => 'require',
               'sheng_id' => 'require|number',
               'shi_id' => 'require|number',
               'xian_id' => 'require|number',
               'address' => 'require',
               'tel' => 'require',
               'channel_id' => 'length:15',
               'coordinate' => 'require',
           ];
        $msg = [
        'title.require' => '店铺名称不能为空',
        'shop_intro.require' => '店铺简介不能为空',
//        'content.require' => '店铺描述不能为空',
        'sheng_id.require' => '省不能为空',
        'sheng_id.number' => '省只能是数字',
        'shi_id.require' => '市不能为空',
        'shi_id.number' => '市只能是数字',
        'xian_id.require' => '区不能为空',
        'xian_id.number' => '区只能是数字',
        'address.require' => '详细地址不能为空',
        'tel.require' => '联系方式不能为空',
        'channel_id.length' => '渠道ID长度15位',
        'coordinate.require' => '坐标不能为空',

        ];
           $validate = new Validate($rule,$msg);
           $validate->scene('add', ['title','shop_intro','sheng_id','shi_id','xian_id','address','tel','channel_id','coordinate']);
           $validate->scene('edit', ['title','shop_intro','sheng_id','shi_id','xian_id','address','tel','channel_id','coordinate']);
//           $validate->scene('edit', ['role_name','id']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        //验证数据
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        //判断用户是否已经添加过店铺
        $admin_id = session('admin_id');
        $res = Db::table('shop_shop')->where('admin_id',$admin_id)->count('id');
        if($res)
        {
            $this->code = -1;
            $this->msg = '只能添加一个店铺';
            return false;
        }
        //处理图片
         $ret = uploadOne('logo', array(
         array(800, 800),
         array(600, 600),
         array(300, 300),
         array(100, 100),
         ));
         if($ret['ok'] == 1)
         {
            $logo = toQiNiu($ret['images'][0]);   // 原图地址
            $mbig_logo = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $big_logo = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
            $mid_logo = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
            $sm_logo = toQiNiu($ret['images'][4]);   // 第三个缩略图地址
            }
         else if($ret['ok'] == 2)
         {
             $logo = '';   // 原图地址
             $mbig_logo = '';   // 第一个缩略图地址
             $big_logo = '';   // 第二个缩略图地址
             $mid_logo = '';   // 第三个缩略图地址
             $sm_logo = '';   // 第三个缩略图地址
         }
         else
         {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
         }

         //二维码图片
        $ret = uploadOne('erweima', array(
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $erweima = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
            $erweima = '';
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }

        //原始数据
        $params = Request::instance()->post(false);
        $id = Db::table('shop_shop')->insertGetId([
            'title'=>$data['title'],
            'admin_id'=>$admin_id,
            'shop_intro'=>$data['shop_intro'],
            'channel_id'=>$data['channel_id'],
            'content'=>$params['content'],
            'contacts'=>$data['contacts'],
            'tel'=>$data['tel'],
            'address'=>$data['address'],
            'logo'=>$logo,
            'sm_logo'=>$sm_logo,
            'mid_logo'=>$mid_logo,
            'big_logo'=>$big_logo,
            'mbig_logo'=>$mbig_logo,
            'sheng_id'=>$data['sheng_id'],
            'shi_id'=>$data['shi_id'],
            'xian_id'=>$data['xian_id'],
            'erweima'=>$erweima,
            'coordinate'=>$data['coordinate'],
            'created_at'=>time(),
            'updated_at'=>time(),
        ]);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '新增失败';
            return false;
        }
        return true;
    }

    /**
     * 全部数据
     */
    public function lst($page = 15)
    {
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        $where = [];
        if($res === false)
        {
            $where['admin_id'] = $admin_id;
        }
        $data = Db::table('shop_shop')->where($where)->field('id,title,channel_id,shop_intro,sm_logo,created_at,updated_at,is_usable')->paginate($page);
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $updateData=[];
        //处理图片
        $ret = uploadOne('logo', array(
            array(800, 800),
            array(600, 600),
            array(300, 300),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $updateData['logo'] = toQiNiu($ret['images'][0]);   // 原图地址
            $updateData['mbig_logo'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $updateData['big_logo'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
            $updateData['mid_logo'] = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
            $updateData['sm_logo'] = toQiNiu($ret['images'][4]);   // 第三个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }

        //二维码图片
        $ret = uploadOne('erweima', array(
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $updateData['erweima'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }
        //原始数据
        $params = Request::instance()->post(false);

        $updateData['title'] = $data['title'];
        $updateData['shop_intro'] = $data['shop_intro'];
        $updateData['channel_id'] = $data['channel_id'];
        $updateData['content'] = $params['content'];
        $updateData['address'] = $data['address'];
        $updateData['tel'] = $data['tel'];
        $updateData['contacts'] = $data['contacts'];
        $updateData['sheng_id'] = $data['sheng_id'];
        $updateData['shi_id'] = $data['shi_id'];
        $updateData['xian_id'] = $data['xian_id'];
        $updateData['coordinate'] = $data['coordinate'];
        $updateData['updated_at'] = time();
        $res = Db::table('shop_shop')->where('id',$data['id'])->update($updateData);
        if(!$res)
        {
            $this->code = -1;
            $this->msg = '修改失败';
            return false;
        }
        return true;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        $res = Db::table('shop_shop')->where('id',$id)->field('is_usable')->find();
        $res['is_usable'] == 1?$zt = 0:$zt = 1;
        return Db::table('shop_shop')->where('id',$id)->update(['is_usable'=>$zt]);
    }
}
